package edu.nf.waimai.dao.admin.impl;

import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.admin.AdminShopDao;
import edu.nf.waimai.model.shop.Shop;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * @Date 2023-09-12
 * @Author qiu
 */
public class AdminShopDaoImpl implements AdminShopDao {
    @Override
    public List<Shop> getAllShop() {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select shop_id,shop_img,shop_types_id,u_id,shop_name,shop_phone,shop_address,shop_datetime from shop;";

        BeanListHandler<Shop> userBeanHandler = new BeanListHandler<>(Shop.class);

        return sqlExecutor.executeQuery(sql, userBeanHandler);
    }

    @Override
    public int updateShop(Shop shop) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "update shop set shop_img = ?,shop_types_id=?,u_id=?,shop_name=?,shop_phone=?,shop_address=? where shop_id = ?";
        return sqlExecutor.executeUpdate(sql, shop.getShopImg(), shop.getShopTypesId(), shop.getShopId(), shop.getShopName(), shop.getShopPhone(), shop.getShopAddress(), shop.getShopId());
    }

    @Override
    public int deleteShop(int uid) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "delete from shop where shop_id = ?";
        return sqlExecutor.executeUpdate(sql, uid);
    }

    @Override
    public int addShop(Shop shop) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "insert into shop(shop_img,shop_types_id,u_id,shop_name,shop_phone,shop_address) values(?,?,?,?,?,?)";
        return sqlExecutor.executeUpdate(sql, shop.getShopImg(), shop.getShopTypesId(), shop.getShopId(), shop.getShopName(), shop.getShopPhone(), shop.getShopAddress());
    }

    @Override
    public List<Shop> getShop() {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select order_id,u_name,shop_name,province,city,district,detail,rider_name,total_price,order_type,order_datetime,order_remarks from orders \n" +
                " left join users on users.u_id = orders.u_id\n" +
                " left join shop on shop.shop_id = orders.shop_id\n" +
                " left join address on address.address_id = orders.address_id\n" +
                " left join rider on rider.rider_id = orders.rider_id \n" +
                " where order_status=6;";
        BeanListHandler<Shop> userHandler = new BeanListHandler<>(Shop.class);
        return sqlExecutor.executeQuery(sql, userHandler);
    }
}
